﻿using Ku.AppCore.Controls;
using Ku.AppCore.Events;
using Prism.Events;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows;

namespace Ku.Modules.Tool.ViewModels
{
    public partial class ClearBinObjViewModel : ViewModelBase
    {
        private readonly IEventAggregator _eventAggregator;
        public ClearBinObjViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }
        #region 消息输出框 
        private void msg(string message)
        {
            try
            {
                _eventAggregator.GetEvent<MessageSentEvent>().Publish(message);

            }
            catch (Exception ex)
            {

            }
        }

        #endregion

        #region 事件

        [ObservableProperty]
        private string _SourceDirectory = "源目录";
        [ObservableProperty]
        private string _CleanDirectory = "bin;obj;.vs;packages";
        /// <summary>
        /// 选择源目录
        /// </summary> 
        [RelayCommand]
        private void SelectedSourcePathClick(object parameter)
        {
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            // 设置初始目录
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.UserProfile;
            var isShowDialog = folderBrowserDialog.ShowDialog();          // 这个方法可以显示文件夹选择对话框
            if (isShowDialog == System.Windows.Forms.DialogResult.OK)
            {
                // 获取选择的文件夹的全路径名
                string directoryPath = folderBrowserDialog.SelectedPath;
                SourceDirectory = directoryPath;
            }
        }

        [RelayCommand]
        private void StartClick(object parameter)
        {
            new Task(() =>
            {
                DoThreadTask();
            }).Start();

        }


        private void DoThreadTask()
        {
            msg("开始执行...");

            // 创建一个新的任务
            Task task = new Task(() =>
            {
                try
                {
                    var filters = CleanDirectory.Replace("；", ";").ToSplit(";");// 需要筛选的目录名称

                    // 获取指定目录及其所有子目录下的所有子目录 
                   string[] subdirectories = Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories);
                    // var subdirectories = Directory.EnumerateDirectories(SourceDirectory, "*", SearchOption.AllDirectories);
                    var count = subdirectories.Count();
                    msg($"目录数量=" + subdirectories.Count());

                    // 使用LINQ查询筛选出需要的子目录
                    var filteredDirectories = subdirectories.Where(dir => filters.Contains(new DirectoryInfo(dir).Name));
                    msg("筛选后的目录=" + filteredDirectories.Count());

                    foreach (string directory in filteredDirectories)
                    {
                        try
                        {
                            Directory.Delete(directory, true);
                            msg(directory + "---删除成功");
                        }
                        catch (Exception ex)
                        {
                            msg(directory + "---删除失败-->" + ex.Message);
                        }


                    }
                }
                catch (Exception ex)
                {
                    msg(ex.ToString());
                }
            });
            msg("启动任务...");
            // 启动任务
            task.Start();
            msg("等待任务...");
            // 等待任务完成
            task.Wait();
            msg("结束执行...");
        }

        [RelayCommand]
        private void StartClick2(object parameter)
        {
            msg("开始执行...");

            // 创建一个新的任务
            Task task = new Task(() =>
            {
                try
                {
                    var filters = CleanDirectory.Replace("；", ";").ToSplit(";");// 需要筛选的目录名称

                    // 获取指定目录及其所有子目录下的所有子目录 
                    //string[] subdirectories = Directory.GetDirectories(SourceDirectory, "*", SearchOption.AllDirectories);
                    var subdirectories = Directory.EnumerateDirectories(SourceDirectory, "*", SearchOption.AllDirectories);
                    var count = subdirectories.Count();
                    msg($"目录数量=" + subdirectories.Count());

                    // 使用LINQ查询筛选出需要的子目录
                    var filteredDirectories = subdirectories.Where(dir => filters.Contains(new DirectoryInfo(dir).Name));
                    msg("筛选后的目录=" + filteredDirectories.Count());

                    foreach (string directory in filteredDirectories)
                    {
                        msg(directory);

                        Thread.Sleep(1);
                    }
                }
                catch (Exception ex)
                {
                    msg(ex.ToString());
                }
            });
            msg("启动任务...");
            // 启动任务
            task.Start();
            msg("等待任务...");
            // 等待任务完成
            task.Wait();
            msg("结束执行...");
        }


        #endregion
    }
}
